<template>
	<view>
		<u-popup mode="bottom" :show="show" round="48rpx" close-on-click-overlay @close="closeHandle">
			<view class="confirm-order-popup">
				<view class="popup-header">
					<text>选择人数</text>
					<image @click="closeHandle" src="../../static/icon/close.png" class="close-icon"></image>
				</view>
				<view class="user-number-list">
					<view class="user-number-item" v-for="(item,index) in roomData">
						<view class="number-label">
							<text>{{ item.label }}</text>
						</view>
						<view class="number-desc">
							<text>{{ item.desc }}</text>
						</view>
						<image class="action-btn active" @click="actionHandle(-1,index)" mode="aspectFill"
							src="../../static/icon/reduce-one.png">
						</image>
						<view class="current-value">
							<text>{{ item.value }}</text>
						</view>
						<image class="action-btn" @click="actionHandle(1,index)" mode="aspectFill"
							src="../../static/icon/add-one.png"></image>
					</view>
					<view class="user-number-item" v-for="(item,index) in hotelData" :key="item.typeId">
						<view class="number-label">
							<text>{{ item.typeName }}</text>
						</view>
						<view class="number-desc">
							<text>{{ item.hotelName }}</text>
						</view>
						<image class="action-btn active" @click="actionHotelHandle(-1,index)" mode="aspectFill"
							src="../../static/icon/reduce-one.png">
						</image>
						<view class="current-value">
							<text>{{ item.value }}</text>
						</view>
						<image class="action-btn" @click="actionHotelHandle(1,index)" mode="aspectFill"
							src="../../static/icon/add-one.png"></image>
					</view>
				</view>
				<view class="confirm-btn">
					<u-button text="确认人数" @click="confirmHandle"></u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		props: ['show'],
		methods: {
			actionHotelHandle(value, index) {
				const { hotelData } = this
				const target = hotelData[index]
				if (!target) return
				const number = target.value + value
				if (number < 1) return this.$toast({ title: '至少选择一间' })
				target.value = number
			},
			initOption(data, cb) {
				this.hotelData = data
				cb && cb()
			},
			validateRoom() {
				const { hotelData } = this
				const len = hotelData.length
				const data = []
				for (let i = 0; i < len; i++) {
					const target = hotelData[i]
					if (!target.value) {
						this.$toast({ title: `请选择${target.desc}的数量` })
						return { flag: false, data }
					}
					const { id, desc, value, price } = target
					target.total = (value * price).toFixed(2)
					data.push(target)
				}
				return { flag: data.length === len, data }
			},
			validateNum() {
				const { roomData } = this
				const data = []
				const { length: len } = roomData
				const options = ['adult_num', 'child_num', 'old_num']
				let count = 0
				for (let i = 0; i < len; i++) {
					const target = roomData[i]
					if (!target) continue
					const key = options[i]
					const { value } = target
					if (!!value) {
						count++
					}
					data.push({
						[key]: value
					})
				}
				return {
					success: !!count,
					data,
				}
			},
			confirmHandle() {
				this.closeHandle()
				const { success, data: numDataList } = this.validateNum()
				if (!success) return this.$toast({ title: '请选择人数' })
				const numData = {}
				numDataList.forEach(item => {
					Object.keys(item).forEach(key => {
						numData[key] = item[key]
					})
				})
				const { flag, data: roomData } = this.validateRoom()
				if (!flag) return
				this.$emit('success', {
					numData,
					roomData,
				})
			},
			closeHandle() {
				this.$emit('close', false)
			},
			actionHandle(value, index) {
				const target = this.roomData[index]
				if (!target) return
				const number = target.value + value
				if (number < 0) return
				target.value = number
			},
		},
		data() {
			return {
				hotelData: [],
				roomData: [{
						label: '成人',
						desc: '12岁以上',
						value: 0,
					},
					{
						label: '儿童',
						desc: '2-12岁(含)',
						value: 0,
					},
					{
						label: '老人',
						desc: '60岁以上',
						value: 0,
					},
					// {
					// 	label: '房间数',
					// 	desc: '',
					// 	value: 0,
					// }
				]
			}
		}
	}
</script>

<style lang="scss" scoped>
	.confirm-order-popup {
		padding: 32rpx 64rpx;
		box-sizing: border-box;
		position: relative;
		background-color: #ffffff;
		border-radius: 48rpx 48rpx 0 0;

		.confirm-btn {
			::v-deep button {
				height: 76rpx;
				background: linear-gradient(135deg, #8BDCFF 0%, #E4FF68 100%);
				border-radius: 100rpx 100rpx 100rpx 100rpx;
				font-size: 28rpx;
				color: rgba(0, 0, 0, 0.9);
			}
		}

		.user-number-list {
			padding: 80rpx 0;
			box-sizing: border-box;
			position: relative;

			.user-number-item {
				display: flex;
				align-items: center;
				margin-bottom: 80rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.current-value {
					width: 40rpx;
					text-align: center;
					height: 44rpx;
					font-size: 28rpx;
					color: rgba(0, 0, 0, 0.9);
					margin: 0 24rpx;
					line-height: 44rpx;
				}

				.number-label {
					height: 48rpx;
					font-size: 32rpx;
					color: rgba(0, 0, 0, 0.9);
					line-height: 48rpx;
				}

				.number-desc {
					height: 40rpx;
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
					line-height: 40rpx;
					margin-left: 24rpx;
				}

				.action-btn {
					width: 48rpx;
					height: 48rpx;

					&.active {
						margin-left: auto;
					}
				}
			}
		}

		.popup-header {
			display: flex;
			justify-content: center;
			position: relative;
			align-items: center;
			height: 48rpx;
			font-size: 32rpx;
			font-weight: bold;
			color: rgba(0, 0, 0, 0.9);
			line-height: 48rpx;

			.close-icon {
				width: 48rpx;
				height: 48rpx;
				position: absolute;
				right: 0;
				top: 50%;
				transform: translateY(-50%);
			}
		}
	}
</style>